package com.feihong.txgw.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.feihong.txgw.R;
import com.feihong.txgw.adapter.Holder.BaseViewHolder;
import com.feihong.txgw.json.HomeJSON;
import com.feihong.txgw.model.bean.home.ProductBaseModel;
import com.feihong.txgw.ui.activity.product.ProductInfoActivity;
import com.feihong.txgw.ui.activity.product.ProductListActivity;
import com.feihong.txgw.utils.ScreenUtil;
import com.feihong.txgw.utils.glidehttps.GlideOptionsSetting;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import static com.feihong.txgw.ui.fragment.home.DiscoverFragment.INTENT_PRODUCTID;

/**
 * @name txgw_app
 * @class name：com.feihong.txgw.adapter
 * @class describe:
 * @author wangziheng
 * @time 2017/9/19 18:15
 * @change
 * @chang time
 * @class describe
 */
public class HomePhoneViewHolder extends BaseViewHolder {
    private TagFlowLayout mTagFlowLayout;
    private TextView mLoadMore;
    private Context mContext;
    private HomeJSON.HomeDataBean mPhoneDataBean;
    private TagAdapter<ProductBaseModel> mFlowLayoutAdapter ;
    private ViewGroup.LayoutParams mParams;
    private LinearLayout.LayoutParams mLayoutParams;
    private TextView mLayoutTitle,mLayoutDes;

    private Drawable drawable ;
    public HomePhoneViewHolder(Context context,View itemView) {
        super(itemView);
        mContext = context;
        drawable = mContext.getResources().getDrawable(R.mipmap.ic_launcher);
        mTagFlowLayout = (TagFlowLayout) itemView.findViewById(R.id.home_phone_flowlayout);
        mLoadMore = (TextView) itemView.findViewById(R.id.home_phone_more_tv);
        mLayoutTitle = (TextView) itemView.findViewById(R.id.home_phone_layout_title);
        mLayoutDes = (TextView) itemView.findViewById(R.id.home_phone_layout_des);
    }

    public void initData(HomeJSON.HomeDataBean homeDataBean){
        mPhoneDataBean = homeDataBean;
        mLayoutTitle.setText(homeDataBean.getList_name());
        mLayoutDes.setText(homeDataBean.getList_des());
        mFlowLayoutAdapter = new TagAdapter<ProductBaseModel>(mPhoneDataBean.getItems()) {
            @Override
            public View getView(FlowLayout parent, int position, ProductBaseModel productBaseModel) {
                View convertView = LayoutInflater.from(mContext).inflate(R.layout.home_phone_flowlayout_item,parent,false);
                TextView phoneTitle = (TextView) convertView.findViewById(R.id.home_phone_float_item_title);
                TextView phoneDescribe = (TextView) convertView.findViewById(R.id.home_phone_float_item_describe);
                ImageView img = (ImageView) convertView.findViewById(R.id.home_phone_float_item_Img);
                LinearLayout mLayout = (LinearLayout) convertView.findViewById(R.id.home_phone_flowlayout_layout);
                mParams = img.getLayoutParams();
                mLayoutParams = (LinearLayout.LayoutParams) mLayout.getLayoutParams();
                int phoneWidth = ScreenUtil.getScreenWidth(mContext);
                double paramsWidth = phoneWidth / 3.5;
                if(productBaseModel.getShowtype().contains("large")){
                    mParams.width = (int) (paramsWidth * 2);
                    mParams.height = (int)(paramsWidth );
                    mLayoutParams.width = (int) (paramsWidth * 2);
                }else if(productBaseModel.getShowtype().contains("small")){
                    mParams.width = (int)(paramsWidth );
                    mParams.height = (int)(paramsWidth );

                    mLayoutParams.width = (int)(paramsWidth );
                }else if(productBaseModel.getShowtype().contains("normal")){
                    mParams.width = (int)(paramsWidth );
                    mParams.height = (int)(paramsWidth );

                    mLayoutParams.width = (int)(paramsWidth);
                }
                mLayout.setLayoutParams(mLayoutParams);
                img.setPadding(0,0,0,0);
                img.setLayoutParams(mParams);
                Glide.with(mContext).load(productBaseModel.getPic()).apply(GlideOptionsSetting.getDefaultOptions(R.mipmap.ic_launcher,0)).into(img);
                phoneTitle.setText(productBaseModel.getName());
                phoneDescribe.setText(productBaseModel.getDes());
                return convertView;
            }
        };

        mTagFlowLayout.setAdapter(mFlowLayoutAdapter);
        mTagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {

                Intent intent = new Intent();
                intent.setClass(mContext, ProductInfoActivity.class);
                intent.putExtra(INTENT_PRODUCTID, mPhoneDataBean.getItems().get(position).getId());
                intent.putExtra(ProductInfoActivity.INTENT_NAME, mPhoneDataBean.getItems().get(position).getName());
                mContext.startActivity(intent);
                return false;
            }
        });
        mLoadMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(mContext, ProductListActivity.class);
                mContext.startActivity(intent);
            }
        });
    }

}
